package com.hangzhou.xyj.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.SimpleUserDao;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.SimpleUserModel;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.net.handler.GetUserDetailHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetUserDetailReq;
import com.hangzhou.xyj.net.response.GetUserDetailResp;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.CommonFollowButton;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.constants.DataKey;
import com.hyphenate.easeui.EaseConstant;

import java.util.List;

/**
 * Created by Administrator on 2016/1/7.
 * 名家主页
 */
public class ExpertInfoActivity extends BaseActivity implements View.OnClickListener {
    String teacherFrom;//师承
    String catStr;//剧种
    String owner;//团长或姓名
    String dramaGroupName;//剧团名称
    String nickName;
    //剧团的弹出View
    View dramaGroupView;
    //演员的弹出View
    View expertView;
    //名家的弹出View
    View expertInfoView;
    //弹出的Dialog
    Dialog dialog;
    private String mTitle;
    private int mIntentTag;//标志位
    private int userId;//用户Id
    private UserModel userInfo;//用户基本信息
    private CircleImageView mCivHeadImg;//头像
    private MyTextView mTvName;//名字
    private MyTextView mTvSign;//签名
    private MyImageView mIvAuthen;//认证图标
    private MyTextView mTvFocusNum;//关注数
    private MyTextView mTvFansNum;//粉丝数
    private MyTextView mTvTeacherOrCategoryText;//师承或者类别
    private MyTextView mTvTeacherFrom;//师承
    private MyTextView mTvIntroduce;//简介
    private MyTextView mTvWorks;//作品
    private CommonFollowButton mBtnFollow;//关注按钮
    private Button mBtnDynamic;//ta的动态
    private Button mBtnConnect;//和ta交流
    private LinearLayout mLlWorks;//更多作品
    private View mVLine;//更多作品上面的分割线
    private LayoutInflater mInflater;

    @Override
    public void initIntentData(Intent intent) {
        super.initIntentData(intent);
        mTitle = intent.getStringExtra(DataKey.TITLE);
        mIntentTag = intent.getIntExtra(DataKey.INTENT_TAG, 0);
        userId = intent.getIntExtra(DataKey.INDEX, -1);
    }

    @Override
    public void initTop() {
        if (StringUtil.isNotEmpty(mTitle)) {
            enableTitle(true, mTitle);
        } else {
            enableTitle(true, "标题");
        }
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_expert_info);
    }

    @Override
    public void initViews() {
//        //先隐藏contentView
//        getContentView().setVisibility(View.INVISIBLE);
        setLoadingAnimVisible(true);

        mLlWorks = findView(R.id.ll_works);
        mVLine = findView(R.id.v_above_works_line);
        mIvAuthen = (MyImageView) findViewById(R.id.miv_authen);
        mCivHeadImg = (CircleImageView) findViewById(R.id.iv_expert_info_head);
        mTvName = (MyTextView) findViewById(R.id.tv_item_username);
        mTvSign = (MyTextView) findViewById(R.id.mtv_sign);
        mTvTeacherFrom = (MyTextView) findViewById(R.id.mtv_teacher);
        mTvFocusNum = (MyTextView) findViewById(R.id.mtv_focus_num);
        mTvFansNum = (MyTextView) findViewById(R.id.mtv_fans_num);
        mTvTeacherOrCategoryText = (MyTextView) findViewById(R.id.mtv_teacher_or_category);
        mTvIntroduce = (MyTextView) findViewById(R.id.mtv_introduce);
        mTvWorks = (MyTextView) findViewById(R.id.mtv_works);
        mBtnFollow = (CommonFollowButton) findViewById(R.id.btn_add_follow);
        mBtnDynamic = (Button) findViewById(R.id.btn_expert_info_head);
        mBtnConnect = (Button) findViewById(R.id.btn_expert_info_chat);
        switch (EIntentTagType.valueOf(mIntentTag)) {
            case TAG_STAGE_EXPERT://名家
                mTvTeacherOrCategoryText.setText("师承： ");
                mIvAuthen.setImageResource(R.drawable.ic_authen_famous_expert);
                break;
            case TAG_STAGE_TEAM://演员
                mTvTeacherOrCategoryText.setText("师承： ");
                mIvAuthen.setImageResource(R.drawable.ic_authen_user);
                break;
            case TAG_DRAMA_GROUP://剧团
                mTvTeacherOrCategoryText.setText("团长： ");
                mIvAuthen.setImageResource(R.drawable.ic_authen_team);
                break;
        }

        if (mIntentTag == EIntentTagType.TAG_STAGE_EXPERT.getValue()) {
            //隐藏  他的动态和 和他交流
            mBtnDynamic.setVisibility(View.GONE);
            mBtnConnect.setVisibility(View.GONE);
        }

    }

    @Override
    public void initDatas() {
        mInflater = LayoutInflater.from(mContext);
        //向服务器请求数据
        GetUserDetailReq getUserDetailReq = null;
        //若登录
        if (mMyUserId != 0) {
            getUserDetailReq = new GetUserDetailReq(userId, mMyUserId);
        } else {//没有登录
            getUserDetailReq = new GetUserDetailReq(userId);
        }
        HttpLoader.getInstance().sendRequest(new GetUserDetailHandler(getUserDetailReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                GetUserDetailResp.UserDetail detail = (GetUserDetailResp.UserDetail) model;
                setData(detail);
            }
        }, 0));
    }

    @Override
    public void initListeners() {
        findView(R.id.btn_expert_info_head).setOnClickListener(this);
        findView(R.id.iv_expert_info_head).setOnClickListener(this);
        findView(R.id.btn_expert_info_chat).setOnClickListener(this);
        findViewById(R.id.mtv_more).setOnClickListener(this);
        findViewById(R.id.miv_authen).setOnClickListener(this);
    }

    /**
     * 填充数据
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/9 17:56
     */
    public void setData(GetUserDetailResp.UserDetail userDetail) {
        userInfo = userDetail.getUserInfo();
        SimpleUserDao dao = new SimpleUserDao(XYJApplication.mAppContext);
        dao.addSimpleUser(new SimpleUserModel(userInfo.getHeadPic(), userInfo.getNickName(), userInfo.getUserId()));
        mCivHeadImg.displayImage(userInfo.getHeadPic());//设置头像
        mTvName.setText(userInfo.getNickName());//设置姓名
        nickName = userInfo.getNickName();//昵称
        mTvSign.setText(userInfo.getSignature());//设置签名
        userInfo.setIsFollow(userDetail.getIsFollow());
        mBtnFollow.setFollow(userDetail.getIsFollow(), Integer.valueOf(userInfo.getUserId()));
        teacherFrom = userInfo.getTeacherFrom();//师承
        catStr = userInfo.getCatStr();//剧种
        owner = userInfo.getOwner();//团长或姓名
        dramaGroupName = userInfo.getBelongOrg();//剧团名称
        switch (EIntentTagType.valueOf(mIntentTag)) {
            case TAG_STAGE_EXPERT://名家
                if (StringUtil.isNotEmpty(teacherFrom)) {
                    mTvTeacherFrom.setText(teacherFrom);
                } else {
                    mTvTeacherFrom.setText("无");
                }
                break;
            case TAG_STAGE_TEAM://演员
                if (StringUtil.isNotEmpty(teacherFrom)) {
                    mTvTeacherFrom.setText(teacherFrom);
                } else {
                    mTvTeacherFrom.setText("无");
                }
                mLlWorks.setVisibility(View.GONE);
                mVLine.setVisibility(View.GONE);
                mTvWorks.setVisibility(View.GONE);
                break;
            case TAG_DRAMA_GROUP://剧团
                if (StringUtil.isNotEmpty(owner)) {
                    mTvTeacherFrom.setText(owner);
                } else {
                    mTvTeacherFrom.setText("无");
                }
                mLlWorks.setVisibility(View.GONE);
                mVLine.setVisibility(View.GONE);
                mTvWorks.setVisibility(View.GONE);
                break;
        }
        mTvFocusNum.setText(String.valueOf(userInfo.getFollowNum()));//设置关注数
        mTvFansNum.setText(String.valueOf(userInfo.getFansNum()));//设置粉丝数
        //设置简介
        if (StringUtil.isEmpty(userInfo.getIntroduce())) {
            mTvIntroduce.setText("无");
        } else {
            mTvIntroduce.setText(userInfo.getIntroduce());
        }
        if ("女".equals(userInfo.getSex())) {
            mBtnDynamic.setText("她的动态");
            mBtnConnect.setText("和她交流");
        }
        //设置作品
        List<GetUserDetailResp.Works> works = userDetail.getWorkList();
        if (works == null || works.size() == 0) {
            mTvWorks.setText("无");
        } else {
            StringBuffer sb = new StringBuffer();
            int i = 0;
            for (; i < works.size() - 1; i++) {
                if (i == 2) break;
                sb.append("《").append(works.get(i).getName()).append("》、");
            }
            sb.append("《").append(works.get(i).getName()).append("》");
            mTvWorks.setText(sb);
        }

        //更新环信映射本地用户数据库
        XYJApplication.getInstance().updateHXUserDB(userInfo);

        //显示contentView
//        getContentView().setVisibility(View.VISIBLE);
        setLoadingAnimVisible(false);
        //剧团的弹出View
        dramaGroupView = mInflater.inflate(R.layout.layout_famous_group_auth_dialog, null);
        ((MyTextView) dramaGroupView.findViewById(R.id.mtv_group_name)).setText(dramaGroupName);
        ((MyTextView) dramaGroupView.findViewById(R.id.mtv_group_leader_name)).setText(owner);
        ((MyTextView) dramaGroupView.findViewById(R.id.mtv_group_cls)).setText(catStr);
        //演员的弹出View
        expertView = mInflater.inflate(R.layout.layout_famous_expert_auth_dialog, null);
        ((MyTextView) expertView.findViewById(R.id.mtv_name)).setText(owner);
        ((MyTextView) expertView.findViewById(R.id.mtv_teacher)).setText(teacherFrom);
        ((MyTextView) expertView.findViewById(R.id.mtv_group_cls)).setText(catStr);
        ((MyTextView) expertView.findViewById(R.id.mtv_belong_org)).setText(dramaGroupName);
        //名家的弹出View
        expertInfoView = mInflater.inflate(R.layout.layout_famous_expert_auth_dialog, null);
        ((MyTextView) expertInfoView.findViewById(R.id.mtv_name)).setText(nickName);
        ((MyTextView) expertInfoView.findViewById(R.id.mtv_teacher)).setText(teacherFrom);
        ((MyTextView) expertInfoView.findViewById(R.id.mtv_group_cls)).setText(catStr);
        expertInfoView.findViewById(R.id.rl_belong_org).setVisibility(View.GONE);

        dialog = new Dialog(mContext, R.style.MyDialogTheme);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        switch (EIntentTagType.valueOf(mIntentTag)) {
            case TAG_DRAMA_GROUP://剧团
                dialog.setContentView(dramaGroupView);
                break;
            case TAG_STAGE_TEAM://演员
                dialog.setContentView(expertView);
                break;
            case TAG_STAGE_EXPERT://名家
                dialog.setContentView(expertInfoView);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            //点击 头像
            case R.id.btn_expert_info_head:
            case R.id.iv_expert_info_head:
                if (mIntentTag == EIntentTagType.TAG_STAGE_EXPERT.getValue()) {
                    return;//如果是名家 不能点击
                }
                Intent intentHomePage = new Intent(this, HomePageActivity.class);
                intentHomePage.putExtra(DataKey.INDEX, Integer.valueOf(userInfo.getUserId()));
                intentHomePage.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_PERSONAL_INFO.getValue());
                intentHomePage.putExtra(DataKey.TITLE, userInfo.getNickName());
                IntentManager.getInstance().goHomePageActivity(mContext, intentHomePage);
                break;
            case R.id.btn_expert_info_chat://点击 “和他交流”
                if (XYJApplication.getUserInfo() == null) {
                    IntentManager.getInstance().goLoginActivity(mContext);
                } else {
                    IntentManager.getInstance().goChatActivity(mContext, userInfo.getUserId(), EaseConstant.CHATTYPE_SINGLE);
                }
                break;
            case R.id.mtv_more://更多作品
                intent = new Intent(mContext, MoreWorksActivity.class);
                intent.putExtra(DataKey.INDEX, userId);
                intent.putExtra(DataKey.TITLE, mTitle);
                intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_STAGE_EXPERT.getValue());//戏台-名家 进入
                IntentManager.getInstance().goActivity(mContext, intent);
                break;
            //点击名字右侧的认证图标
            case R.id.miv_authen:
                dialog.show();
                break;
        }
    }
}
